/*
 * Copyright (c) 2012, Marcin Czekaj
 * All rights reserved.
 *
 * This software is open-source under the BSD license; see LICENSE.TXT for a description.
 */
package pl.edu.uj.fais.gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.KeyStroke;
import javax.swing.event.MenuEvent;

import pl.edu.uj.fais.adapters.MenuAdapter;
import pl.edu.uj.fais.controller.Controller;
import pl.edu.uj.fais.controller.SelectionController;
import pl.edu.uj.fais.mode.GraphMode;

public class MainMenu extends JMenuBar implements ActionListener {
    
    private static final long serialVersionUID = -2489576580384813703L;
    
    private Controller controller = Controller.getInstance();
    
    // File | New
    private JMenuItem newDirectedGraph;
    private JMenuItem newUndirectedGraph;
    private JMenuItem newHyperGraph;
    
    // File
    private JMenuItem open;
    private JMenuItem close;
    private JMenuItem save;
    private JMenuItem saveAs;
    private JMenuItem exit;
    
    // Edit
    private JMenuItem delete;
    private JMenuItem selectAll;
    private JMenuItem editLabel;
    
    // View
    private JMenuItem toggleLabels;
    private JMenuItem zoomIn;
    private JMenuItem zoomOut;
    private JMenuItem resetView;
    
    //Help
    private JMenuItem showHelp;
    
    public MainMenu() {
        initMenuItems();
        updateState(GraphMode.NONE);
    }
    
    public void updateState(GraphMode mode) {
        
        boolean enabled = (mode != GraphMode.NONE);
        
        // File
        close.setEnabled(enabled);
        save.setEnabled(enabled);
        saveAs.setEnabled(enabled);
        
        // Edit
        delete.setEnabled(enabled);
        selectAll.setEnabled(enabled);
        editLabel.setEnabled(false);
        
        // View
        toggleLabels.setEnabled(enabled);
        zoomIn.setEnabled(enabled);
        zoomOut.setEnabled(enabled);
        resetView.setEnabled(enabled);
    }
    
    private void initMenuItems() {
        add(getFileMenu());
        add(getEditMenu());
        add(getViewMenu());
        add(getHelpMenu());
    }

    
    private JMenu getFileMenu() {
        
        JMenu fileMenu = new JMenu("File");
        fileMenu.setMnemonic(KeyEvent.VK_F);
        
        fileMenu.add(getNewSubmenu());
        
        open = new JMenuItem("Open...", KeyEvent.VK_O);
        open.addActionListener(this);
        fileMenu.add(open);
        
        fileMenu.addSeparator();
        
        close = new JMenuItem("Close", KeyEvent.VK_C);
        close.addActionListener(this);
        close.setAccelerator(KeyStroke.getKeyStroke("control W"));
        fileMenu.add(close);
        
        fileMenu.addSeparator();
        
        save = new JMenuItem("Save", KeyEvent.VK_S);
        save.addActionListener(this);
        save.setAccelerator(KeyStroke.getKeyStroke("control S"));
        fileMenu.add(save);
        
        saveAs = new JMenuItem("Save as...", KeyEvent.VK_A);
        saveAs.addActionListener(this);
        fileMenu.add(saveAs);
        
        fileMenu.addSeparator();
        
        exit = new JMenuItem("Exit", KeyEvent.VK_X);
        exit.addActionListener(this);
        fileMenu.add(exit);
        
        return fileMenu;
    }
    
    private JMenu getNewSubmenu() {
        
        JMenu submenu = new JMenu("New");
        submenu.setMnemonic(KeyEvent.VK_N);
        
        newDirectedGraph = new JMenuItem("Directed graph", KeyEvent.VK_D);
        newDirectedGraph.addActionListener(this);
        submenu.add(newDirectedGraph);
        
        newUndirectedGraph = new JMenuItem("Undirected graph", KeyEvent.VK_U);
        newUndirectedGraph.addActionListener(this);
        submenu.add(newUndirectedGraph);
        
        newHyperGraph = new JMenuItem("Hyper graph", KeyEvent.VK_H);
        newHyperGraph.addActionListener(this);
        submenu.add(newHyperGraph);
        
        return submenu;
    }
    
    private JMenu getEditMenu() {
        
        JMenu editMenu = new JMenu("Edit");
        editMenu.setMnemonic(KeyEvent.VK_E);
        editMenu.addMenuListener(new MenuAdapter() {
            
            @Override
            public void menuSelected(MenuEvent event) {
                editLabel.setEnabled(SelectionController.isSingleNodeSelected());
            }
        });
        
        delete = new JMenuItem("Delete", KeyEvent.VK_D);
        delete.addActionListener(this);
        editMenu.add(delete);
        
        selectAll = new JMenuItem("Select all", KeyEvent.VK_S);
        selectAll.addActionListener(this);
        editMenu.add(selectAll);
        
        editMenu.addSeparator();
        
        editLabel = new JMenuItem("Label", KeyEvent.VK_L);
        editLabel.addActionListener(this);
        editMenu.add(editLabel);
        
        return editMenu;
    }
    
    private JMenu getViewMenu() {
        
        JMenu viewMenu = new JMenu("View");
        viewMenu.setMnemonic(KeyEvent.VK_V);
        
        toggleLabels = new JMenuItem("Toggle labels", KeyEvent.VK_L);
        toggleLabels.addActionListener(this);
        viewMenu.add(toggleLabels);
        
        viewMenu.addSeparator();
        
        zoomIn = new JMenuItem("Zoom in", KeyEvent.VK_I);
        zoomIn.addActionListener(this);
        viewMenu.add(zoomIn);
        
        zoomOut = new JMenuItem("Zoom out", KeyEvent.VK_O);
        zoomOut.addActionListener(this);
        viewMenu.add(zoomOut);
        
        viewMenu.addSeparator();
        
        resetView = new JMenuItem("Reset", KeyEvent.VK_R);
        resetView.addActionListener(this);
        viewMenu.add(resetView);
        
        return viewMenu;
    }
    
    
    @Override
    public JMenu getHelpMenu()  {
    	JMenu helpMenu = new JMenu("Help");
    	helpMenu.setMnemonic(KeyEvent.VK_H);
    	showHelp = new JMenuItem("Usage", KeyEvent.VK_U);
    	showHelp.addActionListener(this);
    	helpMenu.add(showHelp);
    	return helpMenu;
    }
    
    //TODO This need to be refactored...
    @Override
    public void actionPerformed(ActionEvent event) {
        
        JMenuItem item = (JMenuItem) event.getSource();
        
        if (item == newDirectedGraph) {
            controller.newDirectedGraph();
        } else if (item == newUndirectedGraph) {
            controller.newUndirectedGraph();
        } else if (item == newHyperGraph) {
            controller.newHyperGraph();
        } else if (item == open) {
            controller.open();
        } else if (item == close) {
            controller.close();
        } else if (item == save) {
            controller.save();
        } else if (item == saveAs) {
            controller.saveAs();
        } else if (item == exit) {
            controller.exit();
        } else if (item == delete) {
            controller.deleteSelected();
        } else if (item == selectAll) {
            controller.selectAll();
        } else if (item == editLabel) {
            controller.editLabel();
        } else if (item == toggleLabels) {
            controller.toggleLabels();
        } else if (item == zoomIn) {
            controller.zoomIn();
        } else if (item == zoomOut) {
            controller.zoomOut();
        } else if (item == resetView) {
            controller.reset();
        } else if (item == showHelp) {
            controller.showHelp();
        }
    }
}
